package org.dromara.pe.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.pe.domain.vo.SportScoreVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.pe.domain.bo.SportScoreBo;
import org.dromara.pe.service.ISportScoreService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 体测成绩管理
 *
 * @author Lion Li
 * @date 2024-11-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pe/sportScore")
public class SportScoreController extends BaseController {

    private final ISportScoreService sportScoreService;

    /**
     * 查询体测成绩管理列表
     */
//    @SaCheckPermission("pe:sportScore:list")
    @GetMapping("/list")
    public TableDataInfo<SportScoreVo> list(SportScoreBo bo, PageQuery pageQuery) {
        return sportScoreService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出体测成绩管理列表
     */
    @SaCheckPermission("pe:sportScore:export")
    @Log(title = "体测成绩管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SportScoreBo bo, HttpServletResponse response) {
        List<SportScoreVo> list = sportScoreService.queryList(bo);
        ExcelUtil.exportExcel(list, "体测成绩管理", SportScoreVo.class, response);
    }

    /**
     * 获取体测成绩管理详细信息
     *
     * @param sportScoreId 主键
     */
    @SaCheckPermission("pe:sportScore:query")
    @GetMapping("/{sportScoreId}")
    public R<SportScoreVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long sportScoreId) {
        return R.ok(sportScoreService.queryById(sportScoreId));
    }

    /**
     * 新增体测成绩管理
     */
    @SaCheckPermission("pe:sportScore:add")
    @Log(title = "体测成绩管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SportScoreBo bo) {
        return toAjax(sportScoreService.insertByBo(bo));
    }

    /**
     * 修改体测成绩管理
     */
    @SaCheckPermission("pe:sportScore:edit")
    @Log(title = "体测成绩管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SportScoreBo bo) {
        return toAjax(sportScoreService.updateByBo(bo));
    }

    /**
     * 删除体测成绩管理
     *
     * @param sportScoreIds 主键串
     */
    @SaCheckPermission("pe:sportScore:remove")
    @Log(title = "体测成绩管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sportScoreIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] sportScoreIds) {
        return toAjax(sportScoreService.deleteWithValidByIds(List.of(sportScoreIds), true));
    }

    /**
     * 查询体测成绩管理列表
     */
//    @SaCheckPermission("pe:sportScore:sync")
    @SaIgnore
    @GetMapping("sync/batch/score")
    public R<?> syncScore(SportScoreBo bo) {
        return toAjax(sportScoreService.batchCreate(bo));
    }
    @SaIgnore
    @GetMapping("sync/export/score")
    public void exportScore(SportScoreBo bo,HttpServletResponse response) {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=scoreTemple.csv");
        response.setCharacterEncoding("UTF-8");
        sportScoreService.exportScore(bo,response);
    }
}
